﻿using System;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
using System.Web.Caching;

using LTJ.Common;
using LTJ.Data;
using LTJ.Config;
using LTJ.Entity;
using LTJ.Common.Generic;
using LTJ.Cache;

namespace LTJ.FrameWork
{
    /// <summary>
    /// 基础数据管理操作
    /// </summary>
    public class BaseDatas
    {
        /// <summary>
        /// 创建基础数据
        /// </summary>
        /// <param name="binfo"></param>
        /// <returns>返回数据ＩＤ，返回-1则数据code与类型code不唯一</returns>
        public static int CreateBaseData(BaseDataInfo binfo)
        {
            if (ExistBaseData(binfo.code, binfo.tcode)) return -1;
            return Data.DataProvider.BaseDatas.CreateBaseData(binfo);
        }

        /// <summary>
        /// 查询当前数据是否唯一
        /// </summary>
        /// <param name="code">数据代码</param>
        /// <param name="tcode">数据类型</param>
        public static bool ExistBaseData(string code, string tcode)
        {
            return Data.DataProvider.BaseDatas.ExistBaseData(code, tcode);
        }

        /// <summary>
        /// 基础数据分页
        /// </summary>
        public static DataTable GetBaseDataByPage(int pageindex, int pagesize, string condition, string orderstr)
        {
            return Data.DataProvider.BaseDatas.GetBaseDataByPage(pageindex, pagesize, condition, orderstr);
        }

        /// <summary>
        /// 基础数据数量
        /// </summary>
        public static int GetBaseDataCount(string conditions)
        {
            return Data.DataProvider.BaseDatas.GetBaseDataCount(conditions);
        }
        public static int GetCompBaseDataCount(string conditions)
        {
            return Data.DataProvider.BaseDatas.GetCompBaseDataCount(conditions);
        }
        public static DataTable GetDestportsMsg(string strWhere)
        {
            return Data.DataProvider.BaseDatas.GetDestportsMsg(strWhere);
        }
        public static DataTable GetCompBaseDataListByStr(int pageindex, int pagesize, string condition, string orderstr)
        {
            return Data.DataProvider.BaseDatas.GetCompBaseDataListByStr(pageindex, pagesize, condition, orderstr);
        }
        

        /// <summary>
        /// 修改基础数据
        /// </summary>
        /// <param name="binfo"></param>
        public static void UpdateBaseData(BaseDataInfo binfo)
        {
            Data.DataProvider.BaseDatas.UpdateBaseData(binfo);
        }

        /// <summary>
        /// 根据ID获取基本数据信息实体
        /// </summary>
        /// <param name="bid"></param>
        /// <returns></returns>
        public static BaseDataInfo GetBaseDataByID(int bid)
        {
            return Data.DataProvider.BaseDatas.GetBaseDataByID(bid);
        }
        /// <summary>
        /// 删除基本数据
        /// </summary>
        /// <param name="ids">基本数据id组合</param>
        /// <returns></returns>
        public static int DelBaseData(string ids)
        {
            return LTJ.Data.DataProvider.BaseDatas.DelBaseData(ids);
        }
        /// <summary>
        /// 根据ID获取基本数据信息实体
        /// </summary>
        /// <param name="bid"></param>
        /// <returns></returns>
        public static BaseDataInfo GetBaseDataByStr(string strWhere)
        {
            return Data.DataProvider.BaseDatas.GetBaseDataByStr(strWhere);
        }
        public static DataTable GetBaseDataListByStr(string strWhere)
        {
            return Data.DataProvider.BaseDatas.GetBaseDataListByStr(strWhere);
        }

        /// <summary>
        /// 港口数据查询条件
        /// </summary>
        /// <param name="searchname"></param>
        /// <param name="searchename"></param>
        /// <param name="routeline">航线</param>
        public static string GetPortDataWhere(string searchname, string searchename, string routeline)
        {
            string rWhere = "code = 'ports'";

            if (routeline != "") rWhere += " AND expand2 = '" + routeline + "'";
            if (searchname != "") rWhere += " AND name LIKE '%" + searchname + "%'";
            if (searchename != "") rWhere += " AND ename LIKE '%" + searchename + "%'";

            return rWhere;
        }
        /// <summary>
        /// 起运港口数据查询条件
        /// </summary>
        public static string GetLoadPortDataWhere(string searchname, string searchename)
        {
            string rWhere = "code = 'ports' AND " + "expand1 = 1";

            if (searchname != "") rWhere += " AND name LIKE '%" + searchname + "%'";
            if (searchename != "") rWhere += " AND ename LIKE '%" + searchename + "%'";

            return rWhere;
        }

        /// <summary>
        /// 中转港口数据查询条件
        /// </summary>
        public static string GetTranPortDataWhere(string searchname, string searchename)
        {
            string rWhere = "code = 'ports'";

            if (searchname != "") rWhere += " AND name LIKE '%" + searchname + "%'";
            if (searchename != "") rWhere += " AND ename LIKE '%" + searchename + "%'";

            return rWhere;
        }
        /// <summary>
        /// 码头数据查询条件
        /// </summary>
        public static string GetDockDataWhere(string searchname, string searchename)
        {
            string rWhere = "code = 'dock'";

            if (searchname != "") rWhere += " AND name LIKE '%" + searchname + "%'";
            if (searchename != "") rWhere += " AND ename LIKE '%" + searchename + "%'";

            return rWhere;
        }
        /// <summary>
        /// 船公司数据查询条件
        /// </summary>
        public static string GetCarrDataWhere(string searchname, string searchename)
        {
            string rWhere = "code = 'carrier'";

            if (searchname != "") rWhere += " AND name LIKE '%" + searchname + "%'";
            if (searchename != "") rWhere += " AND ename LIKE '%" + searchename + "%'";

            return rWhere;
        }
        /// <summary>
        /// 航线数据查询条件
        /// </summary>
        public static string GetSeaRouteDataWhere(string searchname, string searchename)
        {
            return GetSeaRouteDataWhere(searchname, searchename, "", -1);
        }
        /// <summary>
        /// 航线数据查询条件
        /// </summary>
        public static string GetSeaRouteDataWhere(string searchname, string searchename, string boatstr, int cid)
        {
            string rWhere = "code = 'searoute'";

            if (searchname != "") rWhere += " AND name LIKE '%" + searchname + "%'";
            if (searchename != "") rWhere += " AND ename LIKE '%" + searchename + "%'";

            if (cid > 0)
            {
                string hxstr = "";
                List<RouteInfo> rlist = Routes.GetRountListByCidShip(cid, boatstr, true);

                foreach (RouteInfo rinfo in rlist)
                {
                    if (!hxstr.Contains("'" + rinfo.tcode + "'")) hxstr += "'" + rinfo.tcode + "',";
                }
                hxstr = hxstr.Trim(',');
                if (hxstr != "") rWhere += " AND tcode NOT IN (" + hxstr + ")";
            }

            return rWhere;
        }
        /// <summary>
        /// 国家信息获取
        /// </summary>
        public static string GetCountryDataWhere(string searchname, string searchename)
        {
            string rWhere = "code = 'country'";

            if (searchname != "") rWhere += " AND name LIKE '%" + searchname + "%'";
            if (searchename != "") rWhere += " AND ename LIKE '%" + searchename + "%'";

            return rWhere;
        }
        /// <summary>
        /// 返回基础数据查询基本条件
        /// </summary>
        /// <param name="scode">基础数据类型code(必填）</param>
        /// <param name="cid">企业ID</param>
        /// <param name="searchcname">检索中文名</param>
        /// <param name="searchename">检索英文名</param>
        /// <param name="shipcode">船公司code</param>
        /// <param name="type">数据查询类型</param>
        public static string GetBaseDataCondation(string scode, int cid, string searchcname, string searchename, string shipcode, DataMode type)
        {
            string hxstr = "";
            int typevalue = Convert.ToInt16(type);

            if (cid > 0 && typevalue == 1)
            {
                List<RouteInfo> rlist = Routes.GetRountListByCidShip(cid, shipcode, true);

                foreach (RouteInfo rinfo in rlist)
                {
                    if (!hxstr.Contains("'" + rinfo.tcode + "'")) hxstr += "'" + rinfo.tcode + "',";
                }

                if (rlist.Count == 0) hxstr = "'0'";
            }
            else if (cid > 0 && typevalue == 2)
            {
                List<RouteInfo> rlist = Routes.GetRountListByCidShip(cid, shipcode, false);

                foreach (RouteInfo rinfo in rlist)
                {
                    if (!hxstr.Contains("'" + rinfo.bcode + "'")) hxstr += "'" + rinfo.bcode + "',";
                }

                if (rlist.Count == 0) hxstr = "'0'";
            }
            else if (typevalue == Convert.ToInt16(DataMode.RouteArray))
            {
                List<RouteInfo> rlist = Routes.GetRountListByCidShip(cid, shipcode, true);

                foreach (RouteInfo rinfo in rlist)
                {
                    if (!hxstr.Contains("'" + rinfo.tcode + "'")) hxstr += "'" + rinfo.tcode + "',";
                }

                if (rlist.Count == 0) hxstr = "'0'";
            }
            else if (typevalue == Convert.ToInt16(DataMode.PopRoute))
            {
                List<RouteInfo> rlist = Routes.GetRountListByCidShip(cid, shipcode, false);

                foreach (RouteInfo rinfo in rlist)
                {
                    if (!hxstr.Contains("'" + rinfo.tcode + "'")) hxstr += "'" + rinfo.tcode + "',";
                }

                if (rlist.Count == 0) hxstr = "'0'";
            }

            return Data.DataProvider.BaseDatas.GetBaseDataCondation(scode, hxstr.Trim(','), searchcname, searchename, typevalue);
        }
    }
}
